﻿using System;
using System.Data;
using System.Collections.Generic;
using Chenxin.Lib.House.Entity;
namespace Chenxin.Lib.House.BLL
{
    /// <summary>
    /// 业务逻辑类DataDictionary 的摘要说明。
    /// </summary>
    public class DataDictionary
    {

        public DataDictionary()
        { }
        #region  成员方法

        Chenxin.Lib.Cache.CacheService cs = new Chenxin.Lib.Cache.CacheService(Chenxin.Lib.Cache.CacheConn.xCacheConn.DICTDATA);

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Chenxin.Lib.House.Entity.DataDictionary GetModel(Guid RecordId)
        {

            return Chenxin.Lib.House.DAL.DataDictionary.GetModel(RecordId);
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere,string strOrder)
        {
            return Chenxin.Lib.House.DAL.DataDictionary.GetList(strWhere, strOrder);
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<Chenxin.Lib.House.Entity.DataDictionary> GetModelList(string strWhere, string strOrder)
        {
            DataSet ds = Chenxin.Lib.House.DAL.DataDictionary.GetList(strWhere, strOrder);
            return DataTableToList(ds.Tables[0]);
        }

        public List<Chenxin.Lib.House.Entity.DataDictionary> GetDicList(string strKey,Guid gdCity,Guid companyid)
        {
            string strWhere;
            string strOrder;

            strWhere = " ItemKey = '" + strKey + "' And CityId = '" + gdCity.ToString() + "'" + " AND CompanyId = '" + companyid + "'";
            strOrder = " ItemIndex";
            string strCacheKey = string.Format(Chenxin.Lib.Cache.CacheDataType.DICTDATA, strKey + "." + companyid);

            List<Chenxin.Lib.House.Entity.DataDictionary> lsDataDic = cs.GetList<Chenxin.Lib.House.Entity.DataDictionary>(strCacheKey);
            //未命中
            if (!cs.Hit)
            {
                lsDataDic = GetModelList(strWhere, strOrder);
                if (lsDataDic != null && lsDataDic.Count > 0) cs.Set(strCacheKey, lsDataDic);
            }
            return lsDataDic;
        }

        public List<Chenxin.Lib.House.Entity.DataDictionary> GetDicList(string strKey, Guid gdCity)
        {
            string strWhere;
            string strOrder;

            strWhere = " ItemKey = '" + strKey + "' And CityId = '" + gdCity.ToString() + "'";
            strOrder = " ItemIndex";
            string strCacheKey = string.Format(Chenxin.Lib.Cache.CacheDataType.DICTDATA, strKey + "." + gdCity);

            List<Chenxin.Lib.House.Entity.DataDictionary> lsDataDic = cs.GetList<Chenxin.Lib.House.Entity.DataDictionary>(strCacheKey);
            //未命中
            if (!cs.Hit)
            {
                lsDataDic = GetModelList(strWhere, strOrder);
                if (lsDataDic != null && lsDataDic.Count > 0) cs.Set(strCacheKey, lsDataDic);
            }
            return lsDataDic;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<Chenxin.Lib.House.Entity.DataDictionary> DataTableToList(DataTable dt)
        {
            List<Chenxin.Lib.House.Entity.DataDictionary> modelList = new List<Chenxin.Lib.House.Entity.DataDictionary>();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                Chenxin.Lib.House.Entity.DataDictionary model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = new Chenxin.Lib.House.Entity.DataDictionary();
                    if (dt.Rows[n]["RecordId"].ToString() != "")
                    {
                        model.RecordId = new Guid(dt.Rows[n]["RecordId"].ToString());
                    }
                    model.ItemKey = dt.Rows[n]["ItemKey"].ToString();
                    model.ItemValue = dt.Rows[n]["ItemValue"].ToString();
                    if (dt.Rows[n]["ItemType"].ToString() != "")
                    {
                        model.ItemType = int.Parse(dt.Rows[n]["ItemType"].ToString());
                    }
                    if (dt.Rows[n]["ItemIndex"].ToString() != "")
                    {
                        model.ItemIndex = int.Parse(dt.Rows[n]["ItemIndex"].ToString());
                    }
                    if (dt.Rows[n]["CityId"].ToString() != "")
                    {
                        model.CityId = new Guid(dt.Rows[n]["CityId"].ToString());
                    }
                    model.City = dt.Rows[n]["City"].ToString();
                    modelList.Add(model);
                }
            }
            return modelList;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetAllList()
        {
            return GetList("","");
        }

        /// <summary>
        /// 查询分页列表
        /// </summary>
        /// <param name="strWhere">查询条件</param>
        /// <param name="orderField">排序字段</param>
        /// <param name="pageIndex">当前显示的页</param>
        /// <param name="pageSize">当前页显示的记录数</param>
        /// <returns></returns>
        public DataSet GetList(string strWhere,string orderField,int pageIndex,int pageSize,bool doCount)
        {
            return Chenxin.Lib.House.DAL.DataDictionary.GetList("RecordId,ItemKey,ItemValue,ItemType,ItemIndex,CityId,City,ItemRemark", strWhere, orderField, pageIndex, pageSize, doCount);
        }

        #endregion  成员方法
    }
}

